#!/usr/bin/python
# -*- coding: utf-8 -*-
import locale
import os
import sys
import argparse
from xml.etree import ElementTree

preferredEncoding = locale.getpreferredencoding()

#=============================================================================================
class ArgParser:
    __parser = argparse.ArgumentParser(prog="PROG", description=u"Параметры для создания каркаса проекта Maven")
    __args = None

    def __init__(self):
        self.__parser.add_argument('-path', required=True, type=self.__type_path, help=u"путь к папке с проектом")
        self.__parser.add_argument('-config', required=True, type=self.__type_config, help=u"путь к файлу конфигурации проекта")
#        self.__parser.add_argument('-root', required=True, help=u"название проекта")
#        self.__parser.add_argument('-groupId', required=True, help=u"groupId")
#        self.__parser.add_argument('-packageName', required=True, help=u"packageName")
#        self.__parser.add_argument('-modules', nargs='+', required=True, help=u"создаваемые модули")

#        self.__parser.add_argument('-encode', default="UTF-8", help=u"кодировка, по-умолчанию UTF-8")
        self.__args = self.__parser.parse_args()

    def __type_path(self,path):
        if not os.path.exists(path) :
            msg = u"wrong path '%s'" % path
            raise argparse.ArgumentTypeError(msg)
        return path

    def __type_config(self, file):
        if not os.path.isfile(file) :
            msg = u"wrong config file '%s'" % file
            raise argparse.ArgumentTypeError(msg)
        return file


    def args(self):
        return self.__args

#=============================================================================================
class Config:
    __config = {}

    def __init__(self, file):
        print "config file: %s" % file

        tree = ElementTree.parse(file)
        root = tree.getroot()
        self.__readConfig(root)

    def __readConfig(self, node):
        for subelement in node:
            print "\t%s = %s" % (subelement.tag, subelement.attrib)
            self.__readConfig(subelement)

#=============================================================================================
class Maven:

    __archetypeGroupId = u"org.apache.maven.archetypes"
    __archetypeArtifactId = u"maven-archetype-quickstart"

    def __init__(self):
        pass

#=============================================================================================
#=============================================================================================


def main(argv=None):
    try:
        print u"""\n====================================================================
Запуск создания нового проекта.
--------------------------------------
"""
        print "preferredEncoding: ", preferredEncoding
        print "---> ", sys.argv[1:], "\n\n"

        argParser = ArgParser()

        print "### {", argParser.args(), "}"

        config = Config(argParser.args().config)


    except Exception, e:
        print "Exception: ", e
    finally:
         print u"""
--------------------------------------
Завершено создание нового проекта.
====================================================================\n"""

if __name__ == "__main__":
    main()

